var mongodb = require('./mongodb');

var Schema = mongodb.mongoose.Schema;

var MovieSchema = new Schema({
    name           : String,
    alias   	    : [String],
    publish       : Date,
    create_date	  : { type: Date, default: Date.now},
    images        :{
        coverSmall:String,
        coverBig:String
    },
    source        :[{
        source:String,
        link:String,
        swfLink:String,
        quality:String,
        version:String,
        lang:String,
        subtitle:String,
        create_date  : { type: Date, default: Date.now }
    }]

});

var Movie = mongodb.mongoose.model("Movie", MovieSchema);

var MovieDAO = function(){};

//save
MovieDAO.prototype.save = function(obj, callback) {
    var instance = new Movie(obj);
    instance.save(function(err){
        callback(err);
    });
};

//delete
MovieDAO.prototype.delete = function(id,callback){
    Movie.findByIdAndRemove(id,function(err){
        callback(err);
    });
};

//update
MovieDAO.prototype.findByIdAndUpdate = function(obj,callback){
    var _id=obj._id;
    delete obj._id;
    Movie.findOneAndUpdate(_id, obj, function(err,obj){
        callback(err, obj);
    });
}

//findByName
MovieDAO.prototype.findByName = function(name, callback) {
    Movie.find({name:name}, function(err, obj){
        callback(err, obj);
    });
};

//findAll
MovieDAO.prototype.findAll = function(callback){
    Movie.find(function(err,obj){
       callback(err,obj);
    });
};

module.exports = new MovieDAO();